package com.yl.ydk.utils;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.widget.ImageView;

import com.yl.ydk.R;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.transition.Transition;


public class GlideUtils {

    public static void load(Context context, String url, ImageView iv) {
        RequestOptions options = new RequestOptions()
                .placeholder(R.drawable.icon_head);
        Glide.with(context)
                .load(url)
                .apply(options)
                .into(iv);
    }

    public static void load_e8_bg(Context context, String url, ImageView iv) {
        RequestOptions options = new RequestOptions()
                .placeholder(R.color.color_00000000);
        Glide.with(context)
                .load(url)
                .apply(options)
                .into(iv);
    }


    public static void load(Context context, String url, ImageView iv, int placeHolderResId) {
        if (placeHolderResId == -1) {
            Glide.with(context)
                    .load(url)
                    .into(iv);
            return;
        }
        RequestOptions options = new RequestOptions()
                .placeholder(placeHolderResId);
        Glide.with(context)
                .load(url)
                .apply(options)
                .into(iv);
    }

    public static void loadRound(Context context, String url, ImageView iv, int placeResId) {
        RequestOptions options = new RequestOptions()
                .placeholder(placeResId == 0 ? R.drawable.icon_head : placeResId)
                .centerCrop()
                .circleCrop();

        Glide.with(context)
                .load(url)
                .apply(options)
                .into(iv);

    }

    public static void loadRound(Context context, String url, ImageView iv) {
        RequestOptions options = new RequestOptions()
                .placeholder(R.drawable.icon_head)
                .centerCrop()
                .circleCrop();

        Glide.with(context)
                .load(url)
                .apply(options)
                .into(iv);
    }

    public static void loadToBackground(Context context, String url, final View view) {
        Glide.with(context)
                .asBitmap()
                .load(url)
                .into(new SimpleTarget<Bitmap>() {
                    @Override
                    public void onResourceReady(Bitmap resource, Transition<? super Bitmap> transition) {
                        Drawable drawable = new BitmapDrawable(resource);
                        view.setBackground(drawable);
                    }
                });
    }

    public static void loadToBackground(Context context, String url ) {
        Glide.with(context)
                .asBitmap()
                .load(url)
                .into(new SimpleTarget<Bitmap>() {
                    @Override
                    public void onResourceReady(Bitmap resource, Transition<? super Bitmap> transition) {
                        Drawable drawable = new BitmapDrawable(resource);

                    }
                });
    }



}
